Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
How to tell if streams are closed [Node.js]

I have a node js app acting as a middle layer for an ai project I'm working on.

I want to re-route a request (given logic to determine if the request need to be re-routed, and other things). This will be an POST api call (aka no redirects allowed).

Instead of setting up a proxy middleware, i see the request can be piped using the request lib.

However, it seems the post request gets a write error, if i say to close the original POST request stream, it errors saying i cant write to a closed stream. Error: write after end

req.pipe(request({
    url: `${AI_BRAIN_URL}/project/${ai_project_key}`,
    method: 'POST',
    headers: req.headers,
    body: JSON.stringify(req.body)
})).pipe(res)

So, i tell the post request stream to remain open, and to close it in the next pipe .pipe(res, {end:true}.

I figure since pipe merges the streams, the resulting stream should be closed.

Is my assumption correct? How can i tell all the streams are closing?

Final code

async function routeToBrain(req, res, next) {
  const { ai_project_key } = req.body
  const { user_key } = req.user

  // logic to determine routing

  return req.pipe(request({
    url: `${AI_BRAIN_URL}/project/${ai_project_key}`,
    method: 'POST',
    headers: req.headers,
    body: JSON.stringify(req.body)
  }), { end: false }).pipe(res, { end: true })
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!